package com.chaosj.skill.guava
import com.google.common.eventbus.EventBus
import com.google.common.eventbus.Subscribe
/**
 * @Description:
 * @author: caoyangjie
 * @date: 2019年11月2019/11/18日 13:28
 * @Copyright: © 赢时胜
 */
class GuavaEvent {
    static void main(String[] args){
        EventBus bus = new EventBus("GuavaEvent")
        bus.register(new TestEventListener())
        bus.post(new TestEvent("100"))
    }

    static class TestEventListener extends EventListener<TestEvent>{

        @Override
        boolean filter(TestEvent event) {
            return event!=null && event.message == "100"
        }

        @Override
        void handler(TestEvent event) {
            System.out.println(event)
        }
    }

    abstract static class EventListener<E>{

        @Subscribe
        void listen(E event){
            if( filter(event) ){
                handler(event)
            }
        }

        /**
         * 事件过滤
         * @param event
         * @return
         */
        abstract boolean filter(E event)

        /**
         * 处理事件
         * @param event
         */
        abstract void handler(E event)
    }

    /**
     * Guava Event 对象
     */
    static class TestEvent{
        String message

        String getMessage() {
            return message
        }

        TestEvent(String message) {
            this.message = message
        }


        @Override
        String toString() {
            return "TestEvent{" +
                    "message='" + message + '\'' +
                    '}'
        }
    }
}
